package com.sxf.myapplication;

import android.util.Log;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import jcifs.CIFSContext;
import jcifs.Configuration;
import jcifs.config.PropertyConfiguration;
import jcifs.context.BaseContext;
import jcifs.smb.NtlmPasswordAuthenticator;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;

public class MediaHelper {
    private static final String TAG = "MediaHelper";
    BufferedOutputStream bos = null;

    public MediaHelper(String smbPath, String domain, String username, String password)
    {
        try {
            Configuration config = new PropertyConfiguration(System.getProperties());
            CIFSContext context = new BaseContext(config);
            NtlmPasswordAuthenticator auth = new NtlmPasswordAuthenticator(domain, username, password);
            SmbFile smbFile = new SmbFile(smbPath, context.withCredentials(auth));
            OutputStream out = new SmbFileOutputStream(smbFile);
            bos = new BufferedOutputStream(out, 524288);//4194304/2097152/1048576/524288/262144
        } catch (Exception e) {
            Log.e(TAG, "uploadFileToSmb: " + e.toString());
        }
    }

    public int write(byte[] buffer, int length) {
        try {
            bos.write(buffer, 0, length);
            return length;
        } catch (Exception e) {
            Log.e(TAG, "write: " + e.toString());
        }
        return 0;
    }

    public void close() throws IOException {
        bos.close();
    }
}
